{
 "cells": [
  {
   "cell_type": "raw",
   "metadata": {
    "raw_mimetype": "text/restructuredtext"
   },
   "source": [
    ".. _nb_unsga3:"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# U-NSGA-III\n",
    "\n",
    "\n",
    "The algorithm is implemented based on <cite data-cite=\"unsga3\"></cite>. NSGA-III selects parents randomly for mating. It has been shown that tournament selection performs better than random selection. The *U* stands for *unified* and increases NSGA-III's performance by introducing tournament pressure. \n",
    "\n",
    "The mating selections works as follows:"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<div style=\"text-align: center;\">\n",
    "    <img src=\"https://github.com/anyoptimization/pymoo-data/blob/main/docs/images/unsga3_mating.png?raw=true\" width=\"400\">\n",
    "</div>"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Example"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "tags": []
   },
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "\n",
    "from pymoo.algorithms.moo.nsga3 import NSGA3\n",
    "from pymoo.algorithms.moo.unsga3 import UNSGA3\n",
    "from pymoo.problems import get_problem\n",
    "from pymoo.optimize import minimize\n",
    "\n",
    "problem = get_problem(\"ackley\", n_var=30)\n",
    "\n",
    "# create the reference directions to be used for the optimization - just a single one here\n",
    "ref_dirs = np.array([[1.0]])\n",
    "\n",
    "# create the algorithm object\n",
    "algorithm = UNSGA3(ref_dirs, pop_size=100)\n",
    "\n",
    "# execute the optimization\n",
    "res = minimize(problem,\n",
    "               algorithm,\n",
    "               termination=('n_gen', 150),\n",
    "               save_history=True,\n",
    "               seed=1)\n",
    "\n",
    "print(\"UNSGA3: Best solution found: \\nX = %s\\nF = %s\" % (res.X, res.F))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "U-NSGA-III has for single- and bi-objective problems a tournament pressure which is known to be useful.\n",
    "In the following, we provide a quick comparison (here just one run, so not a valid experiment) to see the difference in convergence."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "tags": []
   },
   "outputs": [],
   "source": [
    "_res = minimize(problem,\n",
    "                NSGA3(ref_dirs, pop_size=100),\n",
    "                termination=('n_gen', 150),\n",
    "                save_history=True,\n",
    "                seed=1)\n",
    "print(\"NSGA3: Best solution found: \\nX = %s\\nF = %s\" % (res.X, res.F))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "tags": []
   },
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "import matplotlib.pyplot as plt\n",
    "\n",
    "ret = [np.min(e.pop.get(\"F\")) for e in res.history]\n",
    "_ret = [np.min(e.pop.get(\"F\")) for e in _res.history]\n",
    "\n",
    "plt.plot(np.arange(len(ret)), ret, label=\"unsga3\")\n",
    "plt.plot(np.arange(len(_ret)), _ret, label=\"nsga3\")\n",
    "plt.title(\"Convergence\")\n",
    "plt.xlabel(\"Generation\")\n",
    "plt.ylabel(\"F\")\n",
    "plt.legend()\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "raw_mimetype": "text/restructuredtext"
   },
   "source": [
    "### API"
   ]
  },
  {
   "cell_type": "raw",
   "metadata": {
    "raw_mimetype": "text/restructuredtext"
   },
   "source": [
    ".. autoclass:: pymoo.algorithms.moo.unsga3.UNSGA3\n",
    "    :noindex:"
   ]
  }
 ],
 "metadata": {
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
